<?php

session_start();
require_once '../kelas/record.php';

require_once '../kelas/kategori.php';
require_once '../kelas/produk.php';
require_once '../kelas/amankanParam.php';
require_once '../kelas/fungsi.php';
require_once '../kelas/customer.php';
require_once '../kelas/transaksi.php';
$dtGet = $objParamAman->getMethod();
$dtPost = $objParamAman->postMethod();
//$objCustomer = new customer();
if (isset($dtGet['on'])) {
    $on = $dtGet['on'];
    switch ($on) {
        case "aktifkan":
//        var_dump($dtGet);
        unset($dtGet['on']);
        $objCustomer->update(array('aktif'=>($dtGet['aktif']==1?0:1)),$dtGet);
        $objCustomer->execute();
            echo $objFungsi->redirect(BASEURLADMIN . '?v=customer&p=custlist');
            break;
        case 'login':
            $login = $objCustomer->doLogin($dtPost['username'], $dtPost['password']);
            //echo $objCustomer->strQuery;exit;
            $redStr = 'index.php?v=store';
            if (isset($_SESSION['detail_cart']) && is_array($_SESSION['detail_cart']) && count($_SESSION['detail_cart']) > 0) {
                $redStr = '?v=cart';
            }
            if ($login['success'] == false) {
                if (isset($login['aktif']) && $login['aktif'] == 2) {
                    $redStr = '?v=activateuser&user=' . $login['username'] . '&email=' . $login['email'];
                }
                echo $objFungsi->alert($login['error_msg']);
            };
            echo $objFungsi->redirect(BASEURL . $redStr);
            break;
        case 'resendmail':
            $randAk = rand(1000, 9999);
            $_SESSION['kode_aktifasi'] = $randAk;
            $dtGet['username'] = $dtGet['user'];
            unset($dtGet['user']);
            unset($dtGet['on']);
            $objCustomer->sendMail($dtGet['email'], $randAk, $dtGet['username']);
            $objCustomer->update(array('kode_aktifasi' => $randAk), $dtGet);
            $ex = $objCustomer->execute();
            break;
        case 'cekuser':
            $objCustomer->get('username,email', array(), 2, null, null, null, "(username ='" . $dtPost['username'] . "' OR email = '" . $dtPost['email'] . "')");
            //echo $objCustomer->strQuery;
            $res = $objCustomer->execute();
            if ($res['success'] == true) {
                if ($res['total'] == 2) {
                    $hAkh = array('success' => true, 'error_msg' => 'Username dan Email sudah terdaftar.');
                } else if ($res['total'] == 1) {
                    if ($res['result'][0]['username'] == $dtPost['username']) {
                        $hAkh = array('success' => true, 'duplicate' => true, 'error_msg' => 'Username sudah terdaftar.');
                    } else {
                        $hAkh = array('success' => true, 'duplicate' => true, 'error_msg' => 'Email sudah terdaftar.');
                    }
                } else {
                    $hAkh = array('success' => true, 'duplicate' => false);
                }
            } else {
                $hAkh = $res;
            };
            echo json_encode($hAkh);
            break;
        case 'daftar':
            //////////
            $objCustomer->get('username,email', array(), 2, null, null, null, "(username ='" . $dtPost['username'] . "' OR email = '" . $dtPost['email'] . "')");
            $res = $objCustomer->execute();
            if ($res['success'] == true) {
                if ($res['total'] == 2) {
                    $hAkh = array('success' => false, 'error_msg' => 'Username dan Email sudah terdaftar.');
                } else if ($res['total'] == 1) {
                    if ($res['result'][0]['username'] == $dtPost['username']) {
                        $hAkh = array('success' => false, 'duplicate' => true, 'error_msg' => 'Username sudah terdaftar.');
                    } else {
                        $hAkh = array('success' => false, 'duplicate' => true, 'error_msg' => 'Email sudah terdaftar.');
                    }
                } else {
                    //////////                
                    $dtPost['password'] = sha1($dtPost['password']);
                    $dtPost['waktu'] = date('Y-m-d H:i:s');
                    unset($dtPost['repassword']);
                    $randAk = rand(1000, 9999);
                    $dtPost['kode_aktifasi'] = $randAk; //sha1($randAk);
                    $dtPost['aktif'] = 2;
                    $dtPost['lahir'] = date('Y-m-d', strtotime($dtPost['lahir']));
                    $_SESSION['kode_aktifasi'] = $randAk;
                    $objCustomer->create($dtPost);
                    //echo $objCustomer->strQuery;
                    $objCustomer->sendMail($dtPost['email'], $dtPost['kode_aktifasi'], $dtPost['username']);
                    $hAkh = $objCustomer->execute();

                    //////////
                }
            } else {
                $hAkh = $res;
            };

            if ($hAkh['success'] == false) {
                echo $objFungsi->alert($hAkh['error_msg']);
                echo $objFungsi->redirect(BASEURL . '?v=newaccount');
            } else {
                echo $objFungsi->redirect(BASEURL . '?v=activateuser&user=' . $dtPost['username'] . '&email=' . $dtPost['email']);
            }
            //////////
            break;
        case 'logout':
            $objCustomer->logout();
            echo $objFungsi->redirect(BASEURL);
            break;
        case 'codeactivation':
            $objCustomer->get('*', $dtPost, 1);

            $doAct = $objCustomer->execute();
            if ($doAct['success'] == true && $doAct['total'] == 1) {
                $objCustomer->update(array('aktif' => 1), $dtPost);
                $objCustomer->execute();
                //echo $objCustomer->strQuery;
                //var_dump($doAct);
                $_SESSION['userlogin']['id'] = $doAct['result'][0]['id'];
                $_SESSION['userlogin']['nama'] = $doAct['result'][0]['nama'];
                $_SESSION['userlogin']['username'] = $doAct['result'][0]['username'];
                $_SESSION['userlogin']['email'] = $doAct['result'][0]['email'];
                $_SESSION['userlogin']['alamat'] = $doAct['result'][0]['alamat'];
                $_SESSION['userlogin']['kota'] = $doAct['result'][0]['kota'];
                $_SESSION['userlogin']['telepon'] = $doAct['result'][0]['telepon'];
                //var_dump($_SESSION);
                //exit();
                $hAktifasi = array('success' => true);
            } else {
                $hAktifasi = $doAct['success'] == true ? array('success' => false, 'error_msg' => 'Tidak Ditemukan User Yang Dimaksud') : $doAct;
            };
            $ecC = '';
            $urlA = BASEURL . 'index.php?v=cart';
            if ($hAktifasi['success'] == false) {
                $urlA = BASEURL;
                $ecC .=$objFungsi->alert($hAktifasi['error_msg']);
            };
            echo $ecC . $objFungsi->redirect($urlA); // '<meta http-equiv="refresh" content="0;url=' . $urlA . '">';
            break;
        case 'updatemyprofile':
            $idC = $dtPost['id'];
            unset($dtPost['id']);
            $objCustomer->update($dtPost, array('id' => $idC));
            $e = $objCustomer->execute();
            if ($e['success'] == false) {
                echo $objFungsi->alert($e['error_msg']);
            } else {
                $_SESSION['userlogin']['nama'] = $dtPost['nama'];
            }
            echo $objFungsi->redirect(BASEURL . '?v=myprofile');
            break;
        case 'directActivation':
            $dtGet['kode_aktifasi'] = $dtGet['a'];
            unset($dtGet['a']);
            $objCustomer->num_rows($dtGet); //cek apakah sudah meminta kode aktifasi baru lagi.
            $nrEx = $objCustomer->execute();
            if ($nrEx == 0) {
                $endCact = array('success' => false, 'error_msg' => 'Alamat Aktifasi Ini Tidak Berlaku Lagi');
            } else {
                $objCustomer->update(array('aktif' => 1), $dtGet);
                $endCact = $objCustomer->execute();
            };
            if ($endCact['success'] == false) {
                echo $objFungsi->alert($endCact['error_msg']);
            };
            echo $objFungsi->redirect(BASEURL);
            break;
        case 'chpass':
            $dtPost['id'] = $_SESSION['userlogin']['id'];
            $newPass = sha1($dtPost['password']);
            $dtPost['password'] = sha1($dtPost['oldPass']);
            unset($dtPost['repass']);
            unset($dtPost['oldPass']);
            $objCustomer->num_rows($dtPost);
            $ex = $objCustomer->execute();
            if ($ex['success'] == false || $ex['success'] == true && $ex['result'][0]['total'] == 0) {
                if ($ex['success'] == false) {
                    $end = $ex;
                } else {
                    $end = array('success' => false, 'error_msg' => 'Password Lama Salah');
                }
            } else {
                $objCustomer->update(array('password' => $newPass), $dtPost);
                $end = $objCustomer->execute();
            }
            $redStr = '?v=myprofile';
            if ($end['success'] == false) {
                $redStr = '?v=myprofile&p=chpass';
                echo $objFungsi->alert($end['error_msg']);
            }
            echo $objFungsi->redirect(BASEURL . $redStr);
            break;
        default:
            $objFungsi->inisialisasiProses('');
            break;
    }
} else {
    echo json_encode(array('success' => false, 'error_msg' => 'Direct Access Not Allowed'));
}